var gulp = require('gulp');
var minifyCss = require('gulp-clean-css');
var rename = require('gulp-rename');
var less = require('gulp-less');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var clean = require('gulp-clean');
var autoprefixerOptions = {
    browsers: [
        'last 2 versions',
        'iOS >= 7',
        'Android >= 4',
        'Explorer >= 10',
        'ExplorerMobile >= 11'
    ],
    cascade: false
};

// gulp.task('less', function () {
//     return gulp.src('less/weui.less')
//         .pipe(sourcemaps.init())
//         .pipe(less())
//         .pipe(autoprefixer(autoprefixerOptions))
//         .pipe(minifyCss())
//         .pipe(rename({suffix: '.min'}))
//         .pipe(sourcemaps.write())
//         .pipe(gulp.dest('www/css/'))
//         .pipe(browserSync.stream());
// });

gulp.task('sass', function () {
    gulp.src('scss/**')
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(autoprefixer(autoprefixerOptions))
        .pipe(minifyCss())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('www/css/'))
        .pipe(browserSync.stream());
});

gulp.task('sass2', function () {
    gulp.src('scss2/**')
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(autoprefixer(autoprefixerOptions))
        .pipe(minifyCss())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('www/weixin/css/'))
        .pipe(browserSync.stream());
});

gulp.task('w', ['sass'], function () {
    // gulp.watch("less/**/*.less", ['less']);
    gulp.watch("scss/**/*.scss", ['sass']);
});


gulp.task('serve', ['sass'], function () {

    browserSync.init({
        server: "./www",
        port: 3201,
        host:'0.0.0.0',
        ui: {
            port: 8081,
            host:'0.0.0.0'
        }
    });

    // gulp.watch("less/**/*.less", ['less']);
    gulp.watch("scss/**/*.scss", ['sass']);
    gulp.watch("scss2/**/*.scss", ['sass2']);
    gulp.watch("www/**/*.html").on('change', browserSync.reload);
    gulp.watch("www/**/*.js").on('change', browserSync.reload);
});


gulp.task("clean", function () {
    return gulp.src('cache/www')
        .pipe(clean());
});

gulp.task("build", ['clean','sass'], function () {
    return  gulp.src(["www/**", "sub/release/www/**"])
        .pipe(gulp.dest('cache/wechat/'));
});


gulp.task('default', ["less"]);